package com.example.zaq.rest;

import com.example.zaq.dao.AspKindTextGroupDao;
import com.example.zaq.dao.AspTextGroupDao;
import com.example.zaq.dao.AspTextRecordBodyDao;
import com.example.zaq.dao.AspTextRecordDefaultDao;
import com.example.zaq.model.AspKindTextGroup;
import com.example.zaq.model.AspTextGroup;
import com.example.zaq.model.AspTextRecordBody;
import com.example.zaq.model.AspTextRecordDefault;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 20210524  zaq  有话要说模块
 * 20210525  zaq  完成分组模块调试
 * 20210529  zaq  新增默认类型说说的修改、删除方法
 * 20210606  zaq  完成身体类型说说的增删改查功能
 * @author 阿恩
 * @date 2021/5/2420:53
 */
@RestController
@RequestMapping("/text")
public class AspTextRest {
    @Autowired
    SqlSessionFactory factory;

    /**
     * 查询说说
     * http://localhost:9090/text/getTextBody
     * @param body
     * @return
     */
    @PostMapping("getTextBody")
    @ResponseBody
    public List<AspTextRecordBody> getTextBody(@RequestBody AspTextRecordBody body){
        List<AspTextRecordBody> aspText=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordBodyDao aspTextDao=sess.getMapper(AspTextRecordBodyDao.class);
            aspText=aspTextDao.getAll(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("查询说说结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }


    /**
     * 刪除身体数据类型说说
     * http://localhost:9090/text/deleteTextBody
     * @param body
     * @return
     */
    @PostMapping("deleteTextBody")
    @ResponseBody
    public AspTextRecordBody deleteTextBody(@RequestBody AspTextRecordBody body){
        AspTextRecordBody aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setDate(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordBodyDao aspTextDao=sess.getMapper(AspTextRecordBodyDao.class);
            aspText=aspTextDao.delete(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("删除说说结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 修改身体数据类型说说
     * http://localhost:9090/text/updateTextBody
     * @param body
     * @return
     */
    @PostMapping("updateTextBody")
    @ResponseBody
    public AspTextRecordBody updateTextDefault(@RequestBody AspTextRecordBody body){
        AspTextRecordBody aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setDate(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordBodyDao aspTextDao=sess.getMapper(AspTextRecordBodyDao.class);
            aspText=aspTextDao.update(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("修改说说结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }


    /**
     * 新建身体数据类型说说
     * http://localhost:9090/text/insertTextBody
     * @param body
     * @return
     */
    @PostMapping("insertTextBody")
    @ResponseBody
    public AspTextRecordBody insertTextDefault(@RequestBody AspTextRecordBody body){
        AspTextRecordBody aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setDate(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordBodyDao aspTextDao=sess.getMapper(AspTextRecordBodyDao.class);
            aspText=aspTextDao.insert(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("新增说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }


    /**
     * 删除默认类型说说
     * http://localhost:9090/text/deleteTextDefault
     * @param body
     * @return
     */
    @PostMapping("deleteTextDefault")
    @ResponseBody
    public AspTextRecordDefault deleteTextDefault(@RequestBody AspTextRecordDefault body){
        AspTextRecordDefault aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.settDate(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordDefaultDao aspTextDao=sess.getMapper(AspTextRecordDefaultDao.class);
            aspText=aspTextDao.delete(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("删除说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 修改默认类型说说
     * http://localhost:9090/text/updateTextDefault
     * @param body
     * @return
     */
    @PostMapping("updateTextDefault")
    @ResponseBody
    public AspTextRecordDefault updateTextDefault(@RequestBody AspTextRecordDefault body){
        AspTextRecordDefault aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.settDate(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordDefaultDao aspTextDao=sess.getMapper(AspTextRecordDefaultDao.class);
            aspText=aspTextDao.update(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("修改说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 查询说说
     * http://localhost:9090/text/getTextDefault
     * @param body
     * @return
     */
    @PostMapping("getTextDefault")
    @ResponseBody
    public List<AspTextRecordDefault> getTextDefault(@RequestBody AspTextRecordDefault body){
        List<AspTextRecordDefault> aspText=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordDefaultDao aspTextDao=sess.getMapper(AspTextRecordDefaultDao.class);
            aspText=aspTextDao.getAll(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("查询说说结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 新建默认类型说说
     * http://localhost:9090/text/insertTextDefault
     * @param body
     * @return
     */
    @PostMapping("insertTextDefault")
    @ResponseBody
    public AspTextRecordDefault insertTextDefault(@RequestBody AspTextRecordDefault body){
        AspTextRecordDefault aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.settDate(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextRecordDefaultDao aspTextDao=sess.getMapper(AspTextRecordDefaultDao.class);
            aspText=aspTextDao.insert(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("新增说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 删除分组
     * http://localhost:9090/text/deleteTextGroup
     * @param body
     * @return
     */
    @PostMapping("deleteTextGroup")
    @ResponseBody
    public AspTextGroup deleteTextGroup(@RequestBody AspTextGroup body){
        AspTextGroup aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setgEndTime(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextGroupDao aspTextDao=sess.getMapper(AspTextGroupDao.class);
            aspText=aspTextDao.delete(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("删除说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 修改分组
     * http://localhost:9090/text/updateTextGroup
     * @param body
     * @return
     */
    @PostMapping("updateTextGroup")
    @ResponseBody
    public AspTextGroup updateTextGroup(@RequestBody AspTextGroup body){
        AspTextGroup aspText=null;
        try{
            //获得当前日期
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setgEndTime(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextGroupDao aspTextDao=sess.getMapper(AspTextGroupDao.class);
            aspText=aspTextDao.update(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("修改说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 新建分组
     * http://localhost:9090/text/insertTextGroup
     * @param body
     * @return
     */
    @PostMapping("insertTextGroup")
    @ResponseBody
    public AspTextGroup insertTextGroup(@RequestBody AspTextGroup body){
        AspTextGroup aspText=null;
        try{
            //获得当前日期（用户创建日期）
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setgBeginTime(createDate);

            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextGroupDao aspTextDao=sess.getMapper(AspTextGroupDao.class);
            aspText=aspTextDao.insert(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("新增说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 查询分组
     * http://localhost:9090/text/getTextGroup
     * @param body
     * @return
     */
    @PostMapping("getTextGroup")
    @ResponseBody
    public List<AspTextGroup> getTextGroup(@RequestBody AspTextGroup body){
        List<AspTextGroup> aspText=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspTextGroupDao aspTextDao=sess.getMapper(AspTextGroupDao.class);
            aspText=aspTextDao.getAll(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("查询说说分组结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }

    /**
     * 查询分组模板
     * http://localhost:9090/text/getKindTextGroup
     * @param body
     * @return
     */
    @PostMapping("getKindTextGroup")
    @ResponseBody
    public List<AspKindTextGroup> getKindTextGroup(@RequestBody AspKindTextGroup body){
        List<AspKindTextGroup> aspText=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspKindTextGroupDao aspTextDao=sess.getMapper(AspKindTextGroupDao.class);
            aspText=aspTextDao.getAll(body);
            sess.close();
            System.out.println(aspText);
            System.out.println("查询说说分组模板结束");
            return aspText;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspText;
        }
    }
}
